package com.example.mountainwalker.mapper;

import com.example.mountainwalker.entity.TravelAgencyDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TravelAgencyMapper {

    @Select("<script>" +
            "SELECT COUNT(*) FROM travel_agency ta " +
            "LEFT JOIN region r ON ta.region_id = r.region_id " +
            "<where>" +
            "   <if test='travelAgencyName != null'> AND ta.travel_agency_name LIKE CONCAT('%', #{travelAgencyName}, '%') </if>" +
            "   <if test='officePhone != null'> AND ta.office_phone LIKE CONCAT('%', #{officePhone}, '%') </if>" +
            "   <if test='province != null'> AND r.province = #{province} </if>" +
            "   <if test='city != null'> AND r.city = #{city} </if>" +
            "   <if test='auditStatus != null'> AND ta.audit_status = #{auditStatus} </if>" +
            "</where>" +
            "</script>")
    int getTravelAgencyCountByCondition(
            @Param("travelAgencyName") String travelAgencyName,
            @Param("officePhone") String officePhone,
            @Param("province") String province,
            @Param("city") String city,
            @Param("auditStatus") String auditStatus);

    @Select("<script>" +
            "SELECT " +
            "ta.travel_agency_id as travelAgencyId, " +
            "ta.travel_agency_name as travelAgencyName, " +
            "ta.office_phone as officePhone, " +
            "ta.audit_status as auditStatus, " +
            "ta.registration_time as registrationTime, " +
            "r.province, r.city " +
            "FROM travel_agency ta " +
            "LEFT JOIN region r ON ta.region_id = r.region_id " +
            "<where>" +
            "   <if test='travelAgencyName != null'> AND ta.travel_agency_name LIKE CONCAT('%', #{travelAgencyName}, '%') </if>" +
            "   <if test='officePhone != null'> AND ta.office_phone LIKE CONCAT('%', #{officePhone}, '%') </if>" +
            "   <if test='province != null'> AND r.province = #{province} </if>" +
            "   <if test='city != null'> AND r.city = #{city} </if>" +
            "   <if test='auditStatus != null'> AND ta.audit_status = #{auditStatus} </if>" +
            "</where>" +
            "ORDER BY ta.registration_time DESC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<TravelAgencyDetail> getTravelAgencyListByCondition(
            @Param("travelAgencyName") String travelAgencyName,
            @Param("officePhone") String officePhone,
            @Param("province") String province,
            @Param("city") String city,
            @Param("auditStatus") String auditStatus,
            @Param("offset") int offset,
            @Param("pageSize") int pageSize);

    @Select("SELECT " +
            "ta.travel_agency_id as travelAgencyId, " +
            "ta.travel_agency_name as travelAgencyName, " +
            "ta.office_phone as officePhone, " +
            "ta.audit_status as auditStatus, " +
            "ta.registration_time as registrationTime, " +
            "r.province, r.city " +
            "FROM travel_agency ta " +
            "LEFT JOIN region r ON ta.region_id = r.region_id " +
            "WHERE ta.travel_agency_id = #{id}")
    TravelAgencyDetail getTravelAgencyById(@Param("id") Integer id);

    @Delete("DELETE FROM travel_agency WHERE travel_agency_id = #{id}")
    int deleteTravelAgencyById(@Param("id") Integer id);

    @Delete("<script>" +
            "DELETE FROM travel_agency WHERE travel_agency_id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int batchDeleteTravelAgency(@Param("ids") List<Integer> ids);

    @Update("UPDATE travel_agency SET " +
            "travel_agency_name = #{travelAgencyName}, " +
            "office_phone = #{officePhone}, " +
            "audit_status = #{auditStatus} " +
            "WHERE travel_agency_id = #{travelAgencyId}")
    int updateTravelAgency(TravelAgencyDetail travelAgencyDetail);
}